/**
 * 
 */
package jerry.html;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * Removes trailing extra bytes from the file.
 * 
 * @author (h0t@_G0|i
 *
 */
public class RemoveTrailingBytes {

	/** Clears trailing 00 bytes from a file
	 * 
	 * @param f file
	 */
	public static String clearUp(String fileName) throws IOException{
		try {
			File f = new File(fileName);
			FileInputStream stream = new FileInputStream(f);
			File result = new File(f.getAbsolutePath() + ".clear");
			FileOutputStream outStream = new FileOutputStream(result);
			int available = 0;
			byte[] buffer = new byte[1024];
			while(true) {
				available = stream.read(buffer);
				if(available == -1){
					break;
				}
				byte [] writebuffer = new byte[available/2];
				for(int i = 0; i<available; i+=2){
					writebuffer[i/2] = buffer[i];
				}
				outStream.write(writebuffer);
				outStream.flush();
			}
			stream.close();
			outStream.close();
			f.delete();
			return result.getAbsolutePath();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public static void main(String[] args) throws Exception {
		clearUp("src/Unreal.ngLog.2001.03.15.00.17.40.7777.log");
	}
}
